package com.itzxx.huazhun.ui.record.detail

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.itzxx.huazhun.R
import com.itzxx.huazhun.data.model.DevicesChildBean
import com.itzxx.huazhun.data.model.TestingRecord
import com.itzxx.huazhun.databinding.ActivityRecordDetailBinding
import com.itzxx.huazhun.ui.record.curve.CurveAnalysisActivity
import com.itzxx.library_base_ktx.base.activity.BaseVmDbActivity
import com.itzxx.library_base_ktx.ext.view.addLinearAdapter

class RecordDetailActivity :
    BaseVmDbActivity<RecordDetailViewModel, ActivityRecordDetailBinding>() {

    companion object {
        fun startActivity(context: Context, item: DevicesChildBean) {
            val instent = Intent(
                context,
                RecordDetailActivity()::class.java
            )
            instent.putExtra("data",item)
            context.startActivity(
                instent
            )
        }
    }

    private lateinit var recordDetailAdapter: RecordDetailAdapter

    override fun initView(savedInstanceState: Bundle?) {
        recordDetailAdapter = RecordDetailAdapter()
        mDatabind.rvList.addLinearAdapter(recordDetailAdapter)
        setSupportActionBar(mDatabind.tbHead)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        recordDetailAdapter.addOnItemChildClickListener(R.id.btn_open) { adapter, _, position ->
            val mTestingRecord = adapter.getItem(position) as TestingRecord
            mViewModel.devicesInfoLiveData.value?.let {
                CurveAnalysisActivity.startActivity(
                    this,
                    it.id,
                    mTestingRecord.recordId,
                    it.equipmentNo
                )
            }

        }
    }

    override fun createObserver() {
        mViewModel.run {
            devicesInfoLiveData.observe(this@RecordDetailActivity){
                it.testingRecords?.let {item->
                        if (item.isNotEmpty()){
                            recordDetailAdapter.submitList(item)
                        }
                }
            }
        }
    }

    override fun initData() {
        super.initData()
        val dataParcelableExtra = intent.getParcelableExtra<DevicesChildBean>("data")
        dataParcelableExtra?.let { mViewModel.requestDetail(it) }
    }

    override fun databind2ViewModel() {
        super.databind2ViewModel()
        mDatabind.viewmodel = mViewModel
    }

    override fun onSupportNavigateUp(): Boolean {
        finish()
        return true
    }
}